using FlowerApi.DataBase;
using FlowerApi.Models;
using Microsoft.EntityFrameworkCore;

namespace FlowerApi.Interfaces;

public class FlowerRepository<T> : IFlowerRepository<T> where T : EntityBase
{
    private readonly ApiDbContext _db;
    private DbSet<T> _tb;
    public FlowerRepository(ApiDbContext db)
    {
        _db = db;
        _tb = db.Set<T>();
    }

    public async Task<List<T>> GetAllFlowerAsync()
    {
        return await _tb.ToListAsync();
    }

    public async Task<T?> GetFlowerByIdAsync(Guid guid)
    {

        return await _tb.FindAsync(guid);
    }

    public async Task<T> AddFlowerAsync(T entity)
    {
        var flower = (await _tb.AddAsync(entity)).Entity;
        await _db.SaveChangesAsync();
        return flower;
    }

    public async Task<T> EditFlowerAsync(T entity)
    {
        _tb.Update(entity);
        await _db.SaveChangesAsync();
        return entity;
    }

    public async Task<T> DelFLowerAsync(T entity)
    {
        _tb.Remove(entity);
        await _db.SaveChangesAsync();
        return entity;
    }

}
